#include "rxe.h"
#include "rxe_loc.h"

/*define rdma_user_rxe*/
void rxe_init_av(struct rdma_ah_attr *attr, struct rxe_av *av)
{
    /*主要就是根据应用程序传下来的参数，然后给struct rxe_qp里面的priav成员赋值*/
    rxe_av_from_attr(rdma_ah_get_port_num(attr), av, attr);
    rxe_av_fill_ip_info(av, attr);
    memcpy(av->dmac, attr->roce.dmac, ETH_ALEN);
}

int rxe_av_chk_attr(struct rxe_dev *rxe, struct rdma_ah_attr *attr)
{


}

void rxe_av_from_attr(u8 port_num, struct rxe_av *av,
        struct rdma_ah_attr *attr)
{
    /*指向用户层传下来的参数*/
    const struct ib_global_route *grh = rdma_ah_read_grh(attr);
    /*av--->struct rxe_qp的priav成员*/
    memset(av, 0, sizeof(*av));

    /*给struct rxe_qp结构体的priav成员赋值*/
    memcpy(av->grh.dgid.raw, grh->dgid.raw, sizeof(grh->dgid.raw));
    av->grh.flow_label = grh->flow_label;
    av->grh.sgid_index = grh->sgid_index;
    av->grh.hop_limit = grh->hop_limit;
    av->grh.traffic_class = grh->traffic_class;
    av->port_num = port_num;
}

void rxe_av_to_attr(struct rxe_av *av, struct rdma_ah_attr *attr)
{



}

void rxe_av_fill_ip_info(struct rxe_av *av, struct rdma_ah_attr *attr)
{
    const struct ib_gid_attr *sgid_attr = attr->grh.sgid_attr;
    int ibtype;
    int type;

    rdma_gid2ip((struct sockaddr *)&av->sgid_addr, &sgid_attr->gid);
    rdma_gid2ip((struct sockaddr *)&av->dgid_addr,
            &rdma_ah_read_grh(attr)->dgid);

    ibtype = rdma_gid_attr_network_type(sgid_attr);

    switch (ibtype) {
    case RDMA_NETWORK_IPV4:
        type = RXE_NETWORK_TYPE_IPV4;
        break;
    case RDMA_NETWORK_IPV6:
        type = RXE_NETWORK_TYPE_IPV6;
        break;
    default:
        /* not reached - checked in rxe_av_chk_attr */
        type = 0;
        break;
    }

    /*ipv4或者ipv6*/
    av->network_type = type;
}

struct rxe_av *rxe_get_av(struct rxe_pkt_info *pkt)
{
    if (!pkt || !pkt->qp)
        return NULL;

    if (qp_type(pkt->qp) == IB_QPT_RC || qp_type(pkt->qp) == IB_QPT_UC)
        return &pkt->qp->pri_av;

    return (pkt->wqe) ? &pkt->wqe->av : NULL;
}
